package zozopo.user;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import zozopo.comm.Validate;
import zozopo.user.operations.UserOS;
import zozopo.user.operations.UserTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class SendActiveInfoAction extends ActionSupport implements Action,SessionAware{
	
	private String email;
	private String qq;
	private String valicode;
	
	public String getValicode() {
		return valicode;
	}
	public void setValicode(String valicode) {
		this.valicode = valicode;
	}
	private Map<Object, Object> session;

	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getQq() {
		return qq;
	}
	public void setQq(String qq) {
		this.qq = qq;
	}
	public String execute() {
		// TODO Auto-generated method stub
	
		return SUCCESS;
	}
	public void validate() {
		// TODO Auto-generated method stub
		
		String valicode = (String) session.get("valicode");
		
		if(valicode == null || !valicode.equals(getValicode())){
			addFieldError("errormessage",getText("background.login.valicode.iswrong"));
			return;
		}
		
		if(! Validate.isEmail(email)){
			addFieldError("errormessage","e-mail格式错误请输入正确的e-mail");
			return;
		}
		
		if(Validate.isSpecialCharacters(email)){
			addFieldError("errormessage","不能输入特殊符号  ;\"'\\/*&`+-%?!<>[]{}|");
			return;
		}
		
		if(!Validate.isNumber(qq)){
			addFieldError("errormessage","qq只能是数字");
			return;
		}
		
		if(qq.length() > 20){
			addFieldError("errormessage","QQ号码不能大于20个字符,请重新输入");
			return;
		}
		
		if(email.length() > 128){
			addFieldError("errormessage","email大于128个字符,请重新输入");
			return;
		}
		
		UserOS userOS = new UserOS();
		UserTB user = new UserTB();
		
		user.setUser_email(email);
		user.setUser_name(email);
		user.setUser_qq(qq);
		
		try {
			if(!userOS.isExist(email,qq)){
				addFieldError("errormessage","输入email与qq没找到相应信息");
				return;
			}
			
			if(userOS.isActive(email)){
				addFieldError("errormessage","用户已经激活不需要再次提交激活");
				return;
			}
			userOS.createActiveNumber(user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			addFieldError("errormessage","系统错误");
			return;
		}
		userOS.releaseConnection();
		
		
	}
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
}
